package com.lzb.newloan;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lzb.newloan.bqs.CatLoadingView;
import com.lzb.newloan.utils.BaseActivity;
import com.lzb.newloan.adapter.WaitLoanAdapter;
import com.lzb.newloan.pojo.BorrowPersonPojo;
import com.lzb.newloan.utils.Config;
import com.lzb.newloan.utils.Formatdou;
import com.lzb.newloan.utils.HttpUtils;
import com.lzb.newloan.utils.TimeUtils;
import com.lzb.newloan.xlistview.XListView;
import com.lzb.newloan.xlistview.XListView.IXListViewListener;
import com.lzb.newloan.utils.AlertDialog;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * 正在放款中
 * @author Administrator
 *
 */


public class WaitLoanActivity extends BaseActivity implements IXListViewListener {
	private XListView list_loan;
	private WaitLoanAdapter adapter;
	private ArrayList<BorrowPersonPojo> arrayList;
	private int curPage = 1;
	private boolean isloadMore;
	private String type;
	private String userId;
	private CatLoadingView loadingView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_wait_loan);
		initView();
		initData();
	}

	private Handler mHandler = new Handler() {

		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
				case Config.CODE_INVESTMONEYRECORDE:
					loadingView.dismiss();
					String reString = msg.obj.toString();
					try {
						JSONObject response = new JSONObject(reString);
						JSONObject pageObject = response.getJSONObject("page");
						int totalPages = pageObject.getInt("totalPages");
						if (curPage < totalPages) {
							list_loan.setPullLoadEnable(true);
						} else {
							list_loan.setPullLoadEnable(false);
						}
						ArrayList<BorrowPersonPojo> arrayList2 = new ArrayList<BorrowPersonPojo>();
						JSONArray array = pageObject.getJSONArray("data");
						if (array.length()<=0) {
							new AlertDialog(
									WaitLoanActivity.this).builder().setMsg("您当前没有正在放款的借款用户")
									.setNegativeButton("确定", new OnClickListener() {
										@Override
										public void onClick(View v) {
											finish();
										}
									}).show();
						}else {
							for (int i = 0; i < array.length(); i++) {
								BorrowPersonPojo pojo = new BorrowPersonPojo();
								JSONObject object = (JSONObject) array.get(i);
								pojo.setUsername(object.getString("cardusername"));

								if (type.equals("3")) {//还款时间
									JSONObject hkyq_time = object.getJSONObject("act_repay_time");
									pojo.setTime(TimeUtils.parseDate2(Long.parseLong(hkyq_time.getString("time"))));
								}else if(type.equals("1")){
									pojo.setTime("empty");
								}else{
									JSONObject hkyq_time = object.getJSONObject("sys_repay_time");
									pojo.setTime(TimeUtils.parseDate2(Long.parseLong(hkyq_time.getString("time"))));
								}

								if (object.getString("loan_date").equals("null")) {//放款时间
									JSONObject cl03_time = object.getJSONObject("handle_time");
									pojo.setFkTime(TimeUtils.parseDate2(Long.parseLong(cl03_time.getString("time"))));
								}else {
									JSONObject fkdz_time = object.getJSONObject("loan_date");
									pojo.setFkTime(TimeUtils.parseDate2(Long.parseLong(fkdz_time.getString("time"))));
								}

								double money = object.getDouble("actual_amount");
								double lx = object.getDouble("annual_rate");
								pojo.setSpdz("http://xm-shipin.oss-cn-shenzhen.aliyuncs.com/"+object.getString("video_address"));
								pojo.setSp_money(Formatdou.formatdou(money)+"元");
								pojo.setId(object.getString("id"));

								pojo.setLx(Formatdou.formatdou(lx)+"元");
								arrayList2.add(pojo);
							}

							if (isloadMore) {
								arrayList.addAll(arrayList2);

							} else {
								arrayList = arrayList2;
							}

							adapter.setArrayList(arrayList);
							adapter.notifyDataSetChanged();
						}
					} catch (JSONException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}

					break;
				case Config.CODE_URL_ERROR:
					Toast.makeText(WaitLoanActivity.this, "url错误",
							Toast.LENGTH_SHORT).show();
					break;
				case Config.CODE_NET_ERROR:
					Toast.makeText(WaitLoanActivity.this, "网络错误",
							Toast.LENGTH_SHORT).show();
					break;

				default:
					break;
			}
		};
	};
	
	
	private void initData() {
		HttpUtils.doPostAsyn(Config.INVESTMONEYRECORDE_CORD + "&userid="
				+ userId+"&type="+type+"&curPage="+curPage,"", mHandler, Config.CODE_INVESTMONEYRECORDE);
	}


	private void initView() {
		loadingView = new CatLoadingView(this);
		loadingView.show();
		RelativeLayout backpress = (RelativeLayout) findViewById(R.id.backpress);
		backpress.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		TextView title_txt_center = (TextView) findViewById(R.id.title_txt_center);
		title_txt_center.setText("借款人详情");
		
		type = getIntent().getStringExtra("type");
		SharedPreferences sp = getSharedPreferences("config", 0x0000);
		userId = sp.getString("userid", "");
		
		list_loan = (XListView)findViewById(R.id.list_loan);
		arrayList = new ArrayList<BorrowPersonPojo>();
//		 for (int i = 0; i < 5; i++) {
//				 BorrowPersonPojo pojo = new BorrowPersonPojo();
//				 arrayList.add(pojo);
//				 }
		adapter = new WaitLoanAdapter(this, arrayList,Integer.parseInt(type));
		list_loan.setAdapter(adapter);
		list_loan.setXListViewListener(this);
		list_loan.setPullLoadEnable(false);
		list_loan.setPullRefreshEnable(true);
	}
	
	@Override
	public void onRefresh() {
		mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				// list.clear();
				curPage = 1;
				isloadMore = false;
				initData();
				onLoad();
			}
		}, 2000);
	}

	@Override
	public void onLoadMore() {
		mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				curPage += 1;
				isloadMore = true;
				initData();
				onLoad();
			}
		}, 2000);
	}

	protected void onLoad() {
		list_loan.stopRefresh();
		list_loan.stopLoadMore();
		list_loan.setRefreshTime(TimeUtils.getDate());
	}

	@Override
	public void processMessage(Message message) {
		// TODO Auto-generated method stub
		
	}

}
